import { defineStore } from 'pinia';
import * as u from '@/tool/function/util';
import type { UserState } from '@/tool/lib/interface';
import systemConfig from '@/common/config';
import * as commonUtil from '@/common/utils/util';
let default_userInfo = {} as any;
const userStore = defineStore({
	// 这里的id必须为唯一ID
	id: 'userStore',
	state: () : UserState => {
		return {
			userInfo: default_userInfo,
			userId: default_userInfo?.userId,
			userName: default_userInfo?.userName,
			fullName: default_userInfo?.fullName,
			userType: default_userInfo?.userType,
			menuList: default_userInfo?.menuList || [],
			roles: default_userInfo?.roles || [],
			systemPrivileges: default_userInfo?.systemPrivileges || [],
			orgIds: default_userInfo?.orgIds || [],
			orgId: default_userInfo?.orgId,
			openId: default_userInfo?.openId,
			appId: default_userInfo?.appId,
			accesstoken: u.getCookie(systemConfig.localTokenName),
			refreshToken: u.getCookie(systemConfig.refreshTokenName),
			conversationList: [],
			//定位纬度
			latitude: null,
			//定位精度
			longitude: null,
			//定位地址详情
			address: {},
			areaCode: '',
			areaName: '',
			defaultSM4Key: '',//默认秘钥
			defaultSM2PublicKey: '',//默认公钥
			defaultSM2PrivateKey: ''//默认私钥
		};
	},
	// 等同于vuex的getter
	getters: {},
	// pinia 放弃了 mutations 只使用 actions
	actions: {
		async setToken(token : string | null | undefined, refreshToken : string | null | undefined) {
			let default_zlbFlag = u.getCookie('setZlbFlag') || false;
			if (!default_zlbFlag) {
				let browerInfo = commonUtil.getBrowerInfo();
				if (browerInfo) {
					let type = browerInfo.browserType;
					if (systemConfig.systemType === 'zlb' && (type === 'zlb' || type === 'zlb-alipay' || type === 'alipay' || type === 'weixin')) {
						// 浙里办 判断登录
						default_zlbFlag = true;
					}
				}
			}

			if (!token) {
				u.delCookie(systemConfig.localTokenName);
			}
			if (!refreshToken) {
				u.delCookie(systemConfig.localRefreshTokenName);
			}
			u.setCookie(systemConfig.localTokenName, token);
			u.setCookie(systemConfig.localRefreshTokenName, refreshToken);

			this.accesstoken = token;
			this.refreshToken = refreshToken;
		},
		async setLocation(res : any) {
			this.latitude = res.latitude;
			this.longitude = res.longitude;
			this.address = res.address;
			this.areaCode = res.areaCode;
			this.areaName = res.areaName;
		},
		async setTokenByZlb(token : string | null | undefined, refreshToken : string | null | undefined) {
			this.accesstoken = token;
			this.refreshToken = refreshToken;
		},
		// actions可以用async做成异步形式
		// 设置用户信息
		async setUser(userInfo : any) {
			this.userInfo = userInfo;
			if (userInfo && userInfo.userId) {
				if (userInfo.roles) {
					this.roles = userInfo.roles;
				}
				if (userInfo.systemPrivileges) {
					this.systemPrivileges = userInfo.systemPrivileges;
				}
				this.userType = userInfo.userType;
				this.openId = userInfo.openId;
				this.userName = userInfo.userName;
				this.fullName = userInfo.fullName;
				this.userId = userInfo.userId;
				if (userInfo.orgIds) {
					this.orgIds = userInfo.orgIds;
				}
				this.orgId = userInfo.orgId;
				this.appId = userInfo.appId;
			} else {
				this.roles = [];
				this.systemPrivileges = [];
				this.userType = '';
				this.openId = '';
				this.userName = '';
				this.fullName = '';
				this.orgIds = [];
				this.orgId = '';
				this.appId = '';
				this.userId = '';
				u.delCookie('USER_INFO');
			}
		},
		async setUserSecret(secretInfo : any) {
			if (secretInfo) {
				this.defaultSM2PrivateKey = secretInfo.privateKey;
				this.defaultSM2PublicKey = secretInfo.publicKey;
				this.defaultSM4Key = secretInfo.symmetricKey;
			} else {
				this.defaultSM2PrivateKey = '';
				this.defaultSM2PublicKey = '';
				this.defaultSM4Key = '';
			}
		}
	},
});
export default userStore;